<?php
/**
 *
 * 
 */
class CategoryPreferenceController extends MainController implements ControllerInterface {
	/**
	 * Strona glowna
	 *
	 */
	public function IndexAction($param) {
		// dane do tabelek i paginacji start - user_table
		$table_data = $this->getTableData('preference_table',$param,array('o' => 'name', 'v' => 'malejaco', 't' => 10));
		$v = $table_data['v'] == 'malejaco'?'DESC':'ASC';
		$oTable = array(
			'name' => "preference.name",
			'status' => "preference.status",
			'catalog' => "preference.catalog",
			'choosable' => "preference.choosable",
			'searchable' => "preference.searchable"
		);
		$o = $oTable[$table_data['o']];
		$p = isset($param['strona'])?$param['strona']:1;
		$this->smarty->assign('c',PreferenceDAL::getResult(PreferenceDAL::GetDalDataObj(array('count' => true))));
		//dane do tabelek i paginacji koniec - users_table


		$arrayObjPreferences = PreferenceDAL::GetResult(PreferenceDAL::GetDalDataObj(array(
			'sortBy' => "$o $v",
			'limit' => (($p-1)*$table_data['t']).', '.$table_data['t']
		)));

		$this->smarty->assign('arrayObjPreferences', $arrayObjPreferences);

	}

	/**
	 * Nowa kategoria/branża's
	 *
	 */
	public function AddAction() {

		$categoryObjs = CategoryDAL::GetResult(CategoryDAL::GetDalDataObj(array(

		)));
		$this->smarty->assign('categoryObjs', $categoryObjs);

		$error = $this->ValidateAdd();
		
		$objPreference = new Preference();

		if (Request::GetPost('AddPreference') && empty($error)) {
			
			$objPreference->setName(Request::GetPost('name'));
			
			$description = Request::GetPost('description', false, false);
			$description = str_ireplace('<p>', '', $description);
			$description = str_ireplace('</p>', '', $description);
			
			$objPreference->setDescription($description);
			$objPreference->setCatalog(Request::Check('catalog') ? 1 : 0);
			$objPreference->setStatus(Request::Check('status') ? 1 : 0);
			$objPreference->setChoosable(Request::Check('choosable') ? 1 : 0);
			$objPreference->setSearchable(Request::Check('searchable') ? 1 : 0);
			$objPreference->setWeight(Request::GetPost('weight'));


			$dalObj = PreferenceDAL::GetDalDataObj();
			$dalObj->setObj($objPreference);
			$idPreference = PreferenceDAL::Save($dalObj);


			CategoryPreferencesDAL::Connect2Preference($idPreference,Request::GetPost('categories'));


			$this->AddRedirect(Router::GenerateUrl('CategoryPreferenceIndex', array('CategoryPreference' => 'Index')), 0);
		} else if(Request::GetPost('AddPreference')) {

			$objPreference->setName(Request::GetPost('name'));

			$objPreference->setDescription(Request::GetPost('description', false, false));
			
			$objPreference->setCatalog(Request::Check('catalog') ? 1 : 0);

			$objPreference->setStatus(Request::Check('status') ? 1 : 0);
			$objPreference->setChoosable(Request::Check('choosable') ? 1 : 0);
			$objPreference->setSearchable(Request::Check('searchable') ? 1 : 0);
			$objPreference->setWeight(Request::GetPost('weight'));


			$arrError = array();
			foreach ($error as $err){
				$arrError[$err['field']] = $err['msg'];
			}
			$this->smarty->assign('error', $arrError);

		}

		$this->smarty->assign('objPreference', $objPreference);
		
	}



	/**
	 * Edycja Preference'u
	 *
	 * @param array $param
	 */
	public function EditAction($param) {

		$dalObj = PreferenceDAL::GetDalDataObj(array('id' => $param['id'], 'limit' => 1));
		$objPreference = PreferenceDAL::GetById($dalObj);


		$categoryObjs = CategoryDAL::GetResult(CategoryDAL::GetDalDataObj(array(

		)));
		$this->smarty->assign('categoryObjs', $categoryObjs);

		$categoryPreferences = CategoryPreferencesDAL::GetResult(CategoryPreferencesDAL::getDalDataObj(array(

			'condition' => array(
				'id_preference' => $param['id']
			)

		)));

		$selectedCategories = array();
		foreach($categoryPreferences AS $categoryPreference)
			$selectedCategories[] = $categoryPreference->getIdCategory();
		$this->smarty->assign('selectedCategories', $selectedCategories);



		$error = $this->ValidateEdit();

		if (Request::GetPost('EditPreference') && empty($error)) {

			$objPreference->setName(Request::GetPost('name'));

			$description = Request::GetPost('description', false, false);
			$description = str_ireplace('<p>', '', $description);
			$description = str_ireplace('</p>', '', $description);

			$objPreference->setDescription($description);
			$objPreference->setCatalog(Request::Check('catalog') ? 1 : 0);
			$objPreference->setStatus(Request::Check('status') ? 1 : 0);
			$objPreference->setChoosable(Request::Check('choosable') ? 1 : 0);
			$objPreference->setSearchable(Request::Check('searchable') ? 1 : 0);
			$objPreference->setWeight(Request::GetPost('weight'));


			$dalObj = PreferenceDAL::GetDalDataObj();
			$dalObj->setObj($objPreference);
			$idPreference = PreferenceDAL::Save($dalObj);


			CategoryPreferencesDAL::Connect2Preference($idPreference,Request::GetPost('categories'));

			$objSettings = SettingValueDAL::GetResult(SettingValueDAL::GetDalDataObj(array('condition'=>array('id_preference'=>$param['id']))));
			if(!empty($objSettings)){
				foreach($objSettings as $objSetting){
					$objSetting->setStatus(Request::Check('status') ? 1 : 0);
					$objSetting->setName(Request::GetPost('name'));
					SettingDAL::Save($objSetting->GetDalDataObj());
				}
				
			}
			

			$this->AddRedirect(Router::GenerateUrl('CategoryPreferenceIndex', array('CategoryPreference' => 'Index')), 0);

		} else if(Request::GetPost('EditPreference')) {


			$objPreference->setName(Request::GetPost('name'));

			$objPreference->setDescription(Request::GetPost('description', false, false));

			$objPreference->setCatalog(Request::Check('catalog') ? 1 : 0);
			$objPreference->setStatus(Request::Check('status') ? 1 : 0);
			$objPreference->setChoosable(Request::Check('choosable') ? 1 : 0);
			$objPreference->setSearchable(Request::Check('searchable') ? 1 : 0);
			$objPreference->setWeight(Request::GetPost('weight'));


			$arrError = array();
			foreach ($error as $err){
				$arrError[$err['field']] = $err['msg'];
			}
			$this->smarty->assign('error', $arrError);

		}

		$this->smarty->assign('objPreference', $objPreference);

	}

	/**
	 * Usuwanie Preference'u
	 *
	 * @param array $param
	 */
	public function DeleteAction($param) {
		$fail = 0;
		try {

			$objPreference = new Preference();
			$objPreference->setId($param['id']);

			$dalObj = PreferenceDAL::GetDalDataObj();
			$dalObj->setLimit(1);
			$dalObj->setObj($objPreference);

			PreferenceDAL::Delete($dalObj);
		} catch (Exception $e) {
			$fail = 1;
		}
		$this->AddRedirect(Router::GenerateUrl('CategoryPreferenceIndex', array('CategoryPreference' => 'Index', 'fail' => $fail )), 0);

	}

	public function ToggleStatusAction($param) {
		$this->SetAjaxRender();

		$dalObj = PreferenceDAL::GetDalDataObj(array('id'=> $param['id']));
		$this->content = PreferenceDAL::DefaultToggleFieldById($dalObj, 'status');

	}

	public function ToggleCatalogAction($param) {
		$this->SetAjaxRender();

		$dalObj = PreferenceDAL::GetDalDataObj(array('id'=> $param['id']));
		$this->content = PreferenceDAL::DefaultToggleFieldById($dalObj, 'catalog');

	}

	public function ToggleChoosableAction($param) {
		$this->SetAjaxRender();

		$dalObj = PreferenceDAL::GetDalDataObj(array('id'=> $param['id']));
		$this->content = PreferenceDAL::DefaultToggleFieldById($dalObj, 'choosable');

	}

	public function ToggleSearchableAction($param) {
		$this->SetAjaxRender();

		$dalObj = PreferenceDAL::GetDalDataObj(array('id'=> $param['id']));
		$this->content = PreferenceDAL::DefaultToggleFieldById($dalObj, 'searchable');

	}

	public function AssignPreferenceAction($param) {

		

	}




	public function ValidateAdd(){
		$validator = new Validator(array_merge(Request::GetAllPost(), $_FILES));

		$validator->IsEmpty('name', 'Nazwa nie może być pusta.');
		$validator->IsEmpty('description', 'Opis nie może być pusty.');

		return $validator->GetErrorList();

	}


	public function ValidateEdit(){
		$validator = new Validator(array_merge(Request::GetAllPost(), $_FILES));

		$validator->IsEmpty('name', 'Nazwa nie może być pusta.');
		$validator->IsEmpty('description', 'Opis nie może być pusty.');
		
		return $validator->GetErrorList();
	}

	/**
	 * Wspolna metoda
	 *
	 */
	public function preDispatch($param) {
//		$this->AddScript('jquery-1.4.2.js');
		$this->Run($param);
		$this->RunShared('Auth', array());

	}
	public function postDispatch($param) {

	}
}
?>